package org.itworks.helpers;

import java.sql.*;

public class HsqlDbConnection {
	private Connection handle = null;
	private boolean isOpened = false;
	
	public Connection getHandle() {
		return this.handle;
	}
	
	public HsqlDbConnection(String username, String password, String database) throws Exception {
		// R�cup�rer identifiants dans un fichier
		
		try {
			Class.forName("org.hsqldb.jdbcDriver").newInstance();
			this.handle = DriverManager.getConnection("jdbc:hsqldb:file:" + database, username,  password);
			
			isOpened = true;
		} catch(Exception e) {
			isOpened = false;
			throw e;
		}
	}
	
	public void close() throws SQLException {
		if(isOpened) {
			Statement statement = this.handle.createStatement();
			statement.executeUpdate("SHUTDOWN");
			statement.close();
			handle.close();
			isOpened = false;
		}
	}
	
	public void finalize() {
		try {
			close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
